/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.netconsole.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Romain
 */
public class ProcessusImpl extends UnicastRemoteObject implements Service {

    public ProcessusImpl() throws RemoteException {
    }
    
    public List<Processus> getListProcessus() throws RemoteException {
        List<Processus> listProcess = new ArrayList<>();
        try {            
            Runtime rt = Runtime.getRuntime();
            String[] cmd = {"/bin/bash", "-c", "top -l 1 -ncols 8 | tail -n +13 | sed 's/\\([A-z]\\) \\([A-z]\\)/\\1_\\2/g' | awk '{sub(\"-\",\"\",$1); print $1,$2,$3,$8}' | sed 's/+$//'"};
            Process proc = rt.exec(cmd);
            InputStream stdin = proc.getInputStream();
            InputStreamReader isr = new InputStreamReader(stdin);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            String[] temp;
            System.out.println("<OUTPUT>");
            while ( (line = br.readLine()) != null) {
                System.out.println(line);
                temp = line.split(" ");
                System.out.println(temp[0]);
                Processus p = new Processus();
                p.setPid(Long.parseLong(temp[0]));
                p.setNameProcess(temp[1]);
                p.setCpu(Float.parseFloat(temp[2]));
                p.setMem(temp[3]);
                
                listProcess.add(p);
            }
            System.out.println("</OUTPUT>");
            int exitVal = proc.waitFor();
            System.out.println("Process exitValue: " + exitVal);
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return listProcess;
    }
    
     public float getMemoryUsage() throws RemoteException {
        float usedMem = 0;
        try {            
            Runtime rt = Runtime.getRuntime();
            //Windows:
            String[] cmd = {"/bin/bash", "-c", "echo \"100-`memory_pressure | tail -n 1 | awk '{print $5}' | sed 's/%//'`\" | bc -l"};
            Process proc = rt.exec(cmd);
            InputStream stdin = proc.getInputStream();
            InputStreamReader isr = new InputStreamReader(stdin);
            BufferedReader br = new BufferedReader(isr);

            System.out.println("<OUTPUT>");
            usedMem = Float.parseFloat(br.readLine());
            System.out.println(usedMem);
            System.out.println("</OUTPUT>");
            int exitVal = proc.waitFor();            
            System.out.println("Process exitValue: " + exitVal);
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return usedMem;
    }
     
     public float getCpuUsage() throws RemoteException {
        float usedMem = 0;
        try {            
            Runtime rt = Runtime.getRuntime();
            String[] cmd = {"/bin/bash", "-c", "echo \"100-`top -l 1 -ncols 1 | head -n 4 | tail -n 1 | awk '{ print $7}' | sed 's/%//'`\" | bc -l"};
            Process proc = rt.exec(cmd);
            InputStream stdin = proc.getInputStream();
            InputStreamReader isr = new InputStreamReader(stdin);
            BufferedReader br = new BufferedReader(isr);

            System.out.println("<OUTPUT>");
            usedMem = Float.parseFloat(br.readLine());
            System.out.println(usedMem);
            System.out.println("</OUTPUT>");
            int exitVal = proc.waitFor();            
            System.out.println("Process exitValue: " + exitVal);
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return usedMem;
    }

    public SystemInformation getSystemInformation() throws RemoteException {
        SystemInformation res = new SystemInformation();
        
        res.setCpuUsage(getCpuUsage());
        res.setMemUsage(getMemoryUsage());
        res.setListProcess(getListProcessus());
          
        return res;        
    }
}
